package com.nit.lab.controller.basic;

import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.basic.TeacherDTO;
import com.nit.lab.dto.system.TokenDTO;
import com.nit.lab.dto.utils.OpResultDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.service.basic.TeacherService;
import com.nit.lab.utils.HttpRequestReader;
import com.nit.lab.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @文件名：TeacherController
 * @创建人: wangsheng
 * @创建时间: 2020/3/14
 * @描述:
 */
@RestController
@RequestMapping(value = "/api/teacher")
public class TeacherController {

    @Resource
    private TeacherService teacherService;
    private final Logger logger = LoggerFactory.getLogger(TeacherController.class);

    /**
     *@描述 获取数据表格
     *@创建人  ybj
     *@创建时间  2020/3/28
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            tableRspDTO = teacherService.list4Table(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    /**
     *@描述 添加教师信息
     *@创建人  ybj
     *@创建时间  2020/3/28
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestHeader(name = "Access-Token") String token, @RequestBody TeacherDTO teacherDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
//            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            teacherDTO.setSchoolId(1L);
            opResult.setMessage(teacherService.add(teacherDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     *@描述 验证TeacherCode的唯一性
     *@创建人  ybj
     *@创建时间  2020/3/28
     */
    @RequestMapping(value = "/validateTeacherCode", method = RequestMethod.POST)
    public OpResultDTO validateLoginName(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String teacherCode = jsonRequest.getString("teacherCode");
            if (teacherService.loginCheck(teacherCode) == null) {
                opResult.setMessage("success");
            } else {
                opResult.setMessage("error");
            }
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     *@描述 编辑教师信息
     *@创建人  ybj
     *@创建时间  2020/3/28
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody TeacherDTO teacherDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(teacherService.update(teacherDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
    /**
     *@描述 删除教师信息
     *@创建人  ybj
     *@创建时间  2020/3/28
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String teacherIds = jsonRequest.getString("teacherIds");
            opResult.setMessage(teacherService.remove(teacherIds).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     *@描述 更改密码
     *@创建人  ybj
     *@创建时间  2020/3/28
     */
    @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
    public OpResultDTO resetPwd(@RequestBody TeacherDTO teacherDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(teacherService.resetPwd(teacherDTO.getTeacherId(), teacherDTO.getTeacherPwd()).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 根据Id获取信息
     * @Author: lxr
     * @Date: 2020/03/25
     */
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public OpResultDTO getInfo(@RequestHeader(name = "Access-Token") String token) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            opResult.setMessage("success");
            opResult.setResult(teacherService.getByTeacherId(tokenDTO.getAccountId()));
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    @RequestMapping(value = "/listTeacher", method = RequestMethod.POST)
    public OpResultDTO listTeacher() {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage("success");
            opResult.setResult(teacherService.listTeacher());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
}
